//
//  AppDelegate.swift
//  GZWeibo007
//
//  Created by 黄伟 on 15/12/17.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        //设置导航栏按钮的外观
        setupAppearance()
        
        // TODO: 加载账号,直接调用解档方法
        let userAccount = CZUserAccount.loadUserAccount()
        print("userAccount: \(userAccount)")
        
        // 1.创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // 2.创建tabbar控制器
//        let tabbarVC = CZMainViewController()
        
        // 3.设置root控制器
//        window?.rootViewController = tabbarVC
        window?.rootViewController = defaultViewController()
        window?.backgroundColor = UIColor.whiteColor()
        
        // TODO: 测试欢迎界面
//        window?.rootViewController = CZWelcomeViewController()
        
        // TODO: 测试新特性界面, 别人调用尽量简单
//        window?.rootViewController = CZNewFeatureViewController()
        
        // 4.显示window
        // ?:表示问号前面的值如果为空的话，就不执行后面的代码，整个表达式返回nil
        window?.makeKeyAndVisible()
        
        return true
    }
    
    /// 设置导航栏按钮的外观
    private func setupAppearance() {
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
    }

    /// 判断程序启动后显示的默认控制器
    private func defaultViewController() -> UIViewController {
        // 判断用户是否登录
        if !CZUserAccount.userLogin {
            // 用户没有登录, 返回CZMainViewController对象
            return CZMainViewController()
        }
        
        // 用户已经登录
        return isNewVersion() ? CZNewFeatureViewController() : CZWelcomeViewController()
    }
    
    // 目标控制器有2个
    // 谁来赋值切换AppDelegate,切换界面其实就是切换window的跟控制器.window是AppDelegate的一个属性,AppDelegate来负责切换最合适
    /// 切换程序的根控制器, isMain = true 表示切换到MainViewController, isMain = false 表示切换到WelcomeViewController
    func switchRootViewController(isMain: Bool) {
        window?.rootViewController = isMain ? CZMainViewController() : CZWelcomeViewController()
    }
    
    // MARK: -判断是否是新版本
    /// 判断是否是新版本
    private func isNewVersion() -> Bool {
        // 1. 获取当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        print("当前版本: \(currentVersion)")
        
        // 2.获取上一次保存版本
        let sandboxVersionKey = "sandboxVersionKey" // 保存到沙盒的key
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        print("沙盒版本: \(sandboxVersion)")
        
        // 3. 保存当前版本
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        
        // 4. 返回,当前版本大于上一次的版本
        return currentVersion != sandboxVersion
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

